#include "playertable.h"
#include "mainwindow.h"
#include "villagetile.h"
#include "skillbutton.h"
#include "player.h"
#include "gamedialog.h"
#include <QPainter>
#include <QDebug>



/*


TILE BOARD CLASS


*/


TileBoard::TileBoard(QGraphicsObject *parent):
    QGraphicsObject(parent)
{
    setZValue(zValue()+1);
    for(int i=0; i<3; i++){
        for(int j=0; j<3; j++){
            villages[j][i] = new TeashopTile(this, i, j);
            connect(villages[j][i], SIGNAL(pressed(VillageTile*)), this, SIGNAL(pressed(VillageTile*)));
        }
    }
    villages[0][0] = new SorcererTile(this, 0, 0);
    villages[1][1] = new AltarTile(this, 1, 1);
    villages[0][1] = new PavilionTile(this, 0, 1);

}

QRectF TileBoard::boundingRect() const{
    int width = TileBoard::TILE_WIDTH;
    return QRectF(-width*3/2, -width*3/2, 3*width, 3*width);

}

void TileBoard::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget){
    //QBrush b(QColor(Qt::black), Qt::SolidPattern);
    //painter->fillRect(boundingRect(), b);
}

/*


GHOST SLOT CLASS


*/

GhostSlot::GhostSlot(QGraphicsObject *parent):
    QGraphicsObject(parent)
{
    setAcceptHoverEvents(true);
    _ghostpix = new GhostPixmap(this);
    _ghostpix->scaleForSlotUse(true);
    ghostSetAnim = new GameParAnimation(this);
    ghostSetAnim->addFade(_ghostpix, 0, 1, 400);
    ghostSetAnim->addMove(_ghostpix, QPointF(0, 20), QPointF(0,0), 400);
    currentGhost = 0; // dont forget to intialize all members
}

QRectF GhostSlot::boundingRect() const{
    int slotWidth = GhostBoard::BOARD_WIDTH/3;
    int slotHeight = GhostBoard::BOARD_HEIGHT;
    return QRectF(-slotWidth/2,-slotHeight/2,slotWidth, slotHeight);
}

void GhostSlot::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget){

    bool down = option->state & QStyle::State_Sunken;
    bool over = option->state & QStyle::State_MouseOver;

    QBrush b(QColor(Qt::gray), Qt::SolidPattern);
    painter->fillRect(boundingRect(), b);
    if(down){
        QBrush b(QColor(255, 255, 255, 125), Qt::SolidPattern);

        painter->fillRect(boundingRect(), b);
    }else if(over){
        QBrush b(QColor(255, 255, 255, 125), Qt::SolidPattern);
        painter->fillRect(boundingRect(), b);
    }
    //painter->drawText(boundingRect(), Qt::AlignCenter, "Ghost Slot");
}

void GhostSlot::setGhost(Ghost *ghost){
    currentGhost = ghost;
    _ghostpix->setPixmap(QPixmap(currentGhost->getImagePath()));
    ghostSetAnim->start();
}

void GhostSlot::mousePressEvent(QGraphicsSceneMouseEvent *event){
    update();
    emit pressed(this);
}
void GhostSlot::mouseReleaseEvent(QGraphicsSceneMouseEvent *event){
    update();
}

void GhostSlot::hoverEnterEvent(QGraphicsSceneHoverEvent *event){
    emit hovered( currentGhost );
    update();
}


/*


PLAYER HEAD IMAGE AREA CLASS


*/


PlayerHeadImageArea::PlayerHeadImageArea(QGraphicsObject *parent):
    QGraphicsObject(parent)
{
}

QRectF PlayerHeadImageArea::boundingRect() const{
    return QRectF(0,0, GhostBoard::BOARD_WIDTH, HEAD_HEIGHT);
}


void PlayerHeadImageArea::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget){

    QBrush b(QColor(Qt::white), Qt::SolidPattern);
    painter->fillRect(boundingRect(), b);
    painter->drawText(boundingRect(), Qt::AlignCenter, "PlayerHead");
}

/*


PLAYER STATUS AREA CLASS


*/


PlayerStatusArea::PlayerStatusArea(QGraphicsObject *parent,
                                   player* pl):
    QGraphicsObject(parent),_testPlayer(pl)
{

}


QRectF PlayerStatusArea::boundingRect() const{
    return QRectF(0,0,
                  GhostBoard::BOARD_HEIGHT+PlayerHeadImageArea::HEAD_HEIGHT,
                  STATUS_HEIGHT);
}


void PlayerStatusArea::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget){

    //change the background for different players

    //or maybe the same
    QBrush b(QColor(Qt::red), Qt::SolidPattern);

    painter->fillRect(boundingRect(), b);
    //painter->drawText(boundingRect(), Qt::AlignCenter, "Player Status");

    painter->drawPixmap(10,10,QPixmap( _testPlayer->getImagePath() ));


    painter->drawText(10,60, _testPlayer->getName());
    painter->drawText(75,20, "ID: "+_testPlayer->getID());
    painter->drawText(12,80, "Qi :"+QString::number(_testPlayer->getQi()));
    painter->drawText(12,100, "Ying: "+
                      QString::number(_testPlayer->getYingyang()));
    painter->drawText(12,120, "Budd: "+
                      QString::number(_testPlayer->getBudd()));
    painter->drawText(75,50, "Red fabao :"+
                      QString::number(_testPlayer->getResist0()));
    painter->drawText(75,70, "Yellow fabao :"+
                      QString::number(_testPlayer->getResist1()));
    painter->drawText(75,90, "Blue fabao :"+
                      QString::number(_testPlayer->getResist2()));
    painter->drawText(75,110, "Green fabao :"+
                      QString::number(_testPlayer->getResist3()));
    painter->drawText(75,130, "Black fabao :"+
                      QString::number(_testPlayer->getResist4()));

}



/*


GHOST BOARD CLASS


*/
GhostBoard::GhostBoard(QGraphicsObject *parent, int pn, player* pl):
    QGraphicsObject(parent), _pn(pn)
{
    head = new PlayerHeadImageArea(this);
    status = new PlayerStatusArea(this,pl);
    head->setPos(0, BOARD_HEIGHT);
    status->setPos(BOARD_WIDTH, BOARD_HEIGHT+PlayerHeadImageArea::HEAD_HEIGHT-PlayerStatusArea::STATUS_HEIGHT);

    for(int i=0; i<3; i++){
        slot[i] = new GhostSlot(this);
        slot[i]->setPos(i*BOARD_WIDTH/3+BOARD_WIDTH/3/2, BOARD_HEIGHT/2);
        connect(slot[i], SIGNAL(pressed(GhostSlot*)), this, SIGNAL(slotPressed(GhostSlot*)));
        connect(slot[i], SIGNAL(hovered(Ghost*)), this, SIGNAL(ghostHovered(Ghost*)));
    }

}

QRectF GhostBoard::boundingRect() const{
    return QRectF(0,0, BOARD_WIDTH, BOARD_HEIGHT);
}

void GhostBoard::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget){
    //QBrush b(QColor(Qt::red), Qt::SolidPattern);
    //painter->fillRect(boundingRect(), b);
}


/*


PLAYER TABLE CLASS


*/

PlayerTable::PlayerTable(QGraphicsItem *parent) :
    QGraphicsObject(parent)
{


    tileBoard = new TileBoard(this);

    //create players for the player table
    player* _bPlayer = new player( tileBoard, tr(":/common/images/caocao.png"),
                                  tr("Blue player"), "AAA", player::BLUE,false,
                                0, 0, 0, 0, 0, 0, 1, 3, 1, 1,QPixmap(":/playerIcon/images/red.png") );
    playerList.append(_bPlayer);

    player* _rPlayer = new player( tileBoard, tr(":/common/images/huangzhong.png"),
                                  tr("Red player"), "BBB", player::RED,false,
                                0, 0, 0, 0, 0, 0, 1, 3, 1, 1,QPixmap(":/playerIcon/images/red.png"));
    playerList.append(_rPlayer);

    player* _yPlayer = new player( tileBoard, tr(":/common/images/gaoshun.png"),
                                  tr("Yellow player"), "CCC", player::YELLOW,false,
                                0, 0, 0, 0, 0, 0, 1, 3, 1, 1,QPixmap(":/playerIcon/images/red.png") );
    playerList.append(_yPlayer);

    player* _gPlayer = new player( tileBoard, tr(":/common/images/ganning.png"),
                                  tr("Green player"), "DDD", player::GREEN,false,
                                0, 0, 0, 0, 0, 0, 1, 3, 1, 1,QPixmap(":/playerIcon/images/red.png") );
    playerList.append(_gPlayer);

    organizeGhostBoards();
    connect(tileBoard, SIGNAL(pressed(VillageTile*)), this, SLOT(tileResult(VillageTile*)));

    _respondSlot = false;
    _respondTile = false;
    _respondSkill = false;
}

QRectF PlayerTable::boundingRect() const{
    int tileWidth = TileBoard::TILE_WIDTH;
    int width = 3*tileWidth+GhostBoard::BOARD_HEIGHT*2;
    return QRectF(-width/2,-width/2,
                  3*tileWidth+GhostBoard::BOARD_HEIGHT*2, 3*tileWidth+GhostBoard::BOARD_HEIGHT*2);
}

void PlayerTable::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget){
    //QBrush b(QColor(Qt::yellow), Qt::SolidPattern);
    //painter->fillRect(boundingRect(), b);

}

void PlayerTable::organizeGhostBoards(){
    int tileWidth = TileBoard::TILE_WIDTH;
    QList<player::PlayerColor> colorList;

    colorList << player::YELLOW <<  player::RED
              << player::GREEN << player::BLUE;
    for(int i=0; i<4; i++){
        board[i] = new GhostBoard(this, i,playerList.at(i));
        connect(board[i], SIGNAL(slotPressed(GhostSlot*)), this, SLOT(slotResult(GhostSlot*)));
        connect(board[i], SIGNAL(ghostHovered(Ghost*)), this, SIGNAL(hoveredGhost(Ghost*)));
    }
    //board1
    board[0]->setPos(-tileWidth*3/2, tileWidth*3/2);
    skillButton = new YellowSkillButton(this,playerList[0]);
    connect(skillButton, SIGNAL(pressed(SkillButton*)), this, SLOT(skillResult(SkillButton*)));
    skillButton->setPos(95,255);


    //board2
    board[1]->setPos(tileWidth*3/2, tileWidth*3/2);
    board[1]->rotate(-90);

    //board2
    board[2]->setPos(tileWidth*3/2, -tileWidth*3/2);
    board[2]->rotate(-180);

    //board2
    board[3]->setPos(-tileWidth*3/2, -tileWidth*3/2);
    board[3]->rotate(90);


}

GhostSlot* PlayerTable::askForSlot(){
    _respondSlot = true;
    e.exec();
    _respondSlot = false;
    return askResultSlot;
}

VillageTile* PlayerTable::askForVillageTile(){
    _respondTile = true;
    e.exec();
    _respondTile = false;
    return askResultTile;
}


void PlayerTable::testDialog(){
    GameDialog d(this);
    int test = d.exec();
    qDebug() << test;
}

void PlayerTable::slotResult(GhostSlot *s){
    if( _respondSlot && e.isRunning())
        e.exit();
    askResultSlot = s;
}

void PlayerTable::tileResult(VillageTile *t){
    if( _respondTile && e.isRunning())
        e.exit();
    askResultTile = t;
}

void PlayerTable::skillResult(SkillButton *t){
    if(e.isRunning())
        e.exit();
    askSkillResultTile = t;
}

SkillButton* PlayerTable::askForSkillButton(){
    e.exec();
    return askSkillResultTile;
}
